package com.sctvcloud.renhe.ui.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.view.View;
import android.widget.ImageView;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.OtherUtils;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.ui.activities.ChooseCityActivity;

import java.lang.ref.WeakReference;

/**
 * Created by Ly on 2016/11/28.
 */
public class TitleUtils {

    private CustomFontTextView title, exit;
    private View.OnClickListener backClickListener;

    private ImageView backImg;

    private ImageView locationMid;
    private ImageView logoImg;

    private WeakReference<Activity> activityWeak;
    private View.OnClickListener backClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (backClickListener != null) {
                backClickListener.onClick(view);
            }
            if (activityWeak != null) {
                Activity activity = activityWeak.get();
                if (activity != null) {
                    activity.finish();
                }
            }
        }
    };

    public TitleUtils(Activity activity) {
        activityWeak = new WeakReference<Activity>(activity);
        title = (CustomFontTextView) activity.findViewById(R.id.title_top_title);
        exit = (CustomFontTextView) activity.findViewById(R.id.title_top_edit);
        backImg = (ImageView) activity.findViewById(R.id.title_top_back);
        if (null != backImg) {
            backImg.setOnClickListener(backClick);
        }
        locationMid = (ImageView) activity.findViewById(R.id.title_top_logo_dingwei);
        logoImg = (ImageView) activity.findViewById(R.id.title_top_logo);
//        if (null != logoImg) {
//            logoImg.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Activity ac = OtherUtils.getReferenceObj(activityWeak);
//                    if (ac != null) {
//                        Intent intent = new Intent(ac, ChooseCityActivity.class);
//                        ac.startActivity(intent);
//                    }
//                }
//            });
//        }
//        backLay=activity.findViewById(R.id.title_lay_back);
//        backLay.setOnClickListener(backClick);
    }

    public TitleUtils setBackVisible(boolean isVisible) {
        backImg.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        return this;
    }

    public TitleUtils setTitle(@StringRes int strRes) {
        title.setText(strRes);
        return this;
    }

    public TitleUtils setLogin(boolean isVisible) {
        logoImg.setVisibility(isVisible ? View.GONE : View.VISIBLE);
        title.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        backImg.setVisibility(View.GONE);
        exit.setVisibility(isVisible ? View.GONE : View.VISIBLE);
        locationMid.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        return this;
    }

    public TitleUtils setLogo(boolean isVisible) {
        logoImg.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        locationMid.setVisibility(isVisible ? View.VISIBLE : View.GONE);
//        if (isVisible) {
//            logoImg.setVisibility(View.VISIBLE);
//            title.setVisibility(View.GONE);
//        } else {
//            logoImg.setVisibility(View.GONE);
//            title.setVisibility(View.VISIBLE);
//        }
        return this;
    }

    public TitleUtils setLogo(@DrawableRes int draRes) {
        setLogo(true);
        logoImg.setImageResource(draRes);
        return this;
    }


    public TitleUtils setTitle(String str) {
        if (str == null) {
            title.setText("");
        } else {
            title.setText(str);
        }
        return this;
    }

    public TitleUtils setExit(boolean isVisible, String text) {
        exit.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        exit.setText(text);
        return this;
    }

    public TitleUtils useAndSetTxTitle(String titleStr) {
        setLogo(false);
        title.setVisibility(View.VISIBLE);
        setTitle(titleStr);
        return this;
    }

    public TitleUtils useAndSetTxTitle(int strRes) {
        setLogo(false);
        title.setVisibility(View.VISIBLE);
        setTitle(strRes);
        return this;
    }

    public TitleUtils setBackClickListener(View.OnClickListener backClickListener) {
        this.backClickListener = backClickListener;
        return this;
    }

    public TitleUtils setTitleColor(@ColorRes int colorRes) {
        int color = -1;
        if (activityWeak != null) {
            Activity a = activityWeak.get();
            if (a != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    color = a.getColor(colorRes);
                } else {
                    color = a.getResources().getColor(colorRes);
                }
            }
        }
        if (color != -1) {
            title.setTextColor(color);
        }
        return this;
    }
}
